package com.lorenzogao.pley.ui.presenter

import com.lorenzogao.pley.bean.AppInfo
import com.lorenzogao.pley.bean.BaseBean
import com.lorenzogao.pley.bean.PageBean
import com.lorenzogao.pley.common.rx.RxHttpReponseCompat
import com.lorenzogao.pley.common.rx.subscriber.ErrorHandlerSubscriber
import com.lorenzogao.pley.common.rx.subscriber.ProgressSubcriber
import com.lorenzogao.pley.ui.data.AppInfoModel
import com.lorenzogao.pley.ui.presenter.contract.AppInfoContract
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import javax.inject.Inject

/**
 * Created by GanLanPing
 * data:2017/12/23
 * time:13:26
 * 邮箱:2508719070@qq.com
 * Description:
 */

class AppInfoPresenter @Inject constructor(mModel: AppInfoModel, mView: AppInfoContract.AppInfoView) : BasePresenter<AppInfoModel, AppInfoContract.AppInfoView>(mModel, mView) {

    companion object {
        val TOP_LIST = 1
        val GAME = 2
        val CATEGORY = 3

        val FEAURED = 0
        val TOPLIST = 1
        val NEWLIST = 2
    }


    fun request(type: Int, page: Int, categoryid: Int, flagtype: Int) {


        var subscriber: Observer<PageBean<AppInfo>>? = null


        subscriber = if (page == 0) {
            object : ProgressSubcriber<PageBean<AppInfo>>(mContext!!, mView!!) {
                override fun onNext(t: PageBean<AppInfo>?) {
                    if (t != null) {
                        mView!!.showResult(t)
                    }
                }

            }
        } else {
            object : ErrorHandlerSubscriber<PageBean<AppInfo>>(mContext!!) {
                override fun onSubscribe(d: Disposable?) {
                }

                override fun onComplete() {
                    mView!!.onLoadMoreComplete()
                }

                override fun onNext(t: PageBean<AppInfo>?) {
                    if (t != null) {
                        mView!!.showResult(t)
                    }
                }
            }
        }


        var observable = getObsevable(type, page, categoryid, flagtype)
        observable
                .compose(RxHttpReponseCompat.compatResult())
                .subscribe(subscriber)


    }

    fun requestData(type: Int, page: Int) {
        request(type, page, 0, 0)
    }


    fun requestCategoryApps(page: Int, categoryid: Int, flagtype: Int) {

        request(CATEGORY, page, categoryid, flagtype)
    }

    private fun getObsevable(type: Int, page: Int, categoryid: Int, flagtype: Int): Observable<BaseBean<PageBean<AppInfo>>> {
        return when (type) {
            TOP_LIST -> mModel!!.toplist(page)
            GAME -> mModel!!.games(page)
            CATEGORY -> {
                when (flagtype) {
                    FEAURED -> mModel!!.getFeaturedAppByCategory(categoryid, page)
                    TOPLIST -> mModel!!.getTopListAppByCategory(categoryid, page)
                    else -> mModel!!.getNewListAppByCategory(categoryid, page)

                }

            }

            else -> Observable.empty()
        }

    }


}






